package com.dmcb.trade.business.utils;

import com.dmcb.common.business.utils.JsonUtil;
import com.dmcb.common.business.utils.WebUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by miting on 2018/10/22.
 */


public class TengXunCrawlUtil {

    public static Map<String, String> getTengXunInfo(String link) {
        String data;
        Map<String, String> map1 = new HashMap<>();
        Map<String, Object> map;//https://new.qq.com/omn/20181002/20181002a0s2uy00
        String id;
        if (link.contains(".html")) {
            id = link.substring(link.lastIndexOf("/") + 1);
            id = id.substring(0, id.lastIndexOf(".")) + "00";
        } else if (link.contains("?")) {
            id=link.substring(link.lastIndexOf("/") + 1);
            id=id.substring(0,id.lastIndexOf("?"))+"00";
        } else {
            id = link.substring(link.lastIndexOf("/") + 1);
            if (id.length() < 16) {
                id = link.substring(link.lastIndexOf("/") + 1) + "00";
            }
        }
        String realLink = "https://openapi.inews.qq.com/getQQNewsNormalContent?id=" + id + "&chlid=news_rss&refer=mobilewwwqqcom&otype=jsonp&ext_data=all&srcfrom=newsapp&callback=getNewsContentOnlyOutput";
        data = WebUtil.buildRequest(realLink, false);
        data = data.replace("getNewsContentOnlyOutput(", "");
        data = data.substring(0, data.lastIndexOf(")"));
        map = JsonUtil.getResult(data);
        if (map == null || map.size() == 0) {
            return null;
        }
        String title = (String) map.get("title");
        map = (Map) map.get("ext_data");
        if (map == null) {
            return null;
        }
        String content = map.get("cnt_html") + "";
//        map1.put("content", content);
        map1.put("title", title);
        return map1;
    }

}
